﻿using BayNexususer.XMLModel;
using Common;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;

namespace NursingFormSystem.BLL
{
    public class SynchroBLL
    {
        public delegate void templateInfoSyncToEMRDelegate(Temuser temuser);
        public delegate void clinicalNursingInfoAsyncToEMRDelegate(ClinicalNursinguser clinicalNursing, string bloodsugarxmlStr, string formType, string insertflag);
        public delegate void bloodsugarInfoASyncToEMRDelegate(ClinicalNursinguser clinicalNursing, string bloodsugarxmlStr, string insertflag);

        #region 泉州体温单回写EMR库
        /// <summary>
        /// 体温单委托方法回写EMR库
        /// </summary>
        /// <param name="Temuser"></param>
        public void templateInfoSyncToEMR(Temuser temuser)
        {
            templateInfoSyncToEMRDelegate asyncDelegate = new templateInfoSyncToEMRDelegate(templateInfoasyncToEMR);
            asyncDelegate.BeginInvoke(temuser, null, null);
        }

        /// <summary>
        /// 体温单异步回写EMR库方法
        /// </summary>
        /// <param name="temuser"></param>
        public void templateInfoasyncToEMR(Temuser temuser)
        {
            try
            {
                YDHLService.ManageZYServiceService insertEMRService = new YDHLService.ManageZYServiceService();
                string result = insertEMRService.intoEMR(temuser.data);
                XmlDocument doc = XmlUtilTool.ConvertStringToXmlDocument(result);
                XmlNode codeNode = doc.SelectSingleNode("message/response/code");
                XmlNode descriptionNode = doc.SelectSingleNode("message/response/description");
                if (codeNode != null)
                {
                    if (!codeNode.InnerText.Equals("0"))
                    {
                        string returnString = string.Format("病人名-{0},测量时间{1},PC端体温单回写EMR库成功", temuser.PatientId.Trim(), System.DateTime.Now.ToString("yyyymmdd"));
                        WriteLog.WriteLogString(returnString);
                    }
                }
            }
            catch (Exception)
            {

            }

        }
        /// <summary>
        /// 体温单同步到护理记录单并将数据回写EMR库
        /// </summary>
        /// <param name="insertflag">新增1，修改2，删除3</param>
        /// <param name="temuser"></param>
        public void specialBillInfoSyncToEMR(Temuser temuser, string insertflag)
        {
            TemperatureDocument temDoc = XmlUtilTool.DeserializeToObject<TemperatureDocument>(temuser.data);
            if (temDoc.FooterMeasures.Count > 0)
            {
                SpecialbillInfo specInfoObj = new SpecialbillInfo();
                List<specialbill> specialbillList = new List<specialbill>();
                foreach (FooterMeasure footerMea in temDoc.FooterMeasures)
                {
                    specialbills specialbills = new specialbills();
                    specialbill specialbillObj = new specialbill();
                    List<intake> intakeList = new List<intake>();
                    List<discharge> dischargeList = new List<discharge>();
                    List<specialbillele> specialbilleleList = new List<specialbillele>();
                    request requestObj = new request();
                    requestObj.targetLogicService = "EMR_SPECIAL_BILL_INFO";
                    string specialconfigsn = " ";
                    /*
                     * 001，（神内）护理记录单；002，（全院通用）护理记录单；
                     * 100，（神内）24小时入出量；101，（神内）12小时入出量；
                     * 200，（全院通用）24小时入出量；201，（全院通用）12小时入出量；
                     * */
                    if (temuser.AreaName.Contains("神经内科"))
                    {
                        requestObj.targetLogicApp = "001";
                        specialconfigsn = "13630069866070145882";
                    }
                    else
                    {
                        specialconfigsn = "13539761728360413173";
                        requestObj.targetLogicApp = "002";
                    }
                    specialbillObj.intake = intakeList;
                    specialbillObj.discharge = dischargeList;
                    specialbillObj.specialbillele = specialbilleleList;
                    specialbillObj.insertflag = insertflag;
                    specialbillObj.nodetype = "0";//0,普通; 1,24小时总出入量; 2,24小时尿量; 3,24时出量; 4,12时总入出量;*5,12小时尿量; 6,12小时出量;
                    specialbillObj.specialconfigsn = specialconfigsn;
                    specialbillObj.specialsn = System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "1";//特护单记录流水号唯一值
                    specialbillObj.inpatientid = temuser.PatientId.TrimEnd() + temuser.VisitId + "_" + temuser.VisitId;
                    specialbillObj.patientid = temuser.PatientId.TrimEnd();
                    specialbillObj.creator = temuser.UpdateuserId;
                    specialbillObj.creatortime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                    specialbillObj.modifier = temuser.UpdateuserId;
                    specialbillObj.modifiertime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                    specialbillObj.recordtime = footerMea.ValueDate.ToString("yyyyMMddHHmmss");
                    specialbillObj.recorder = temuser.UpdateuserName;
                    specialbillObj.tendcontent = " ";
                    specialbillObj.deptcode = temuser.DepId;
                    specialbillObj.deptname = temuser.DepName;
                    specialbillObj.wardname = temuser.AreaName;
                    specialbillObj.wardcode = temuser.AreaId;
                    if (temDoc.Measures.Count > 0)
                    {
                        List<tempfrae> tempfraeList = new List<tempfrae>();
                        foreach (Measure measure in temDoc.Measures)
                        {
                            if ((footerMea.ValueDate - measure.ValueDate).Days == 0)
                            {
                                specialbillObj.temper = measure.Body_Temperature;
                                specialbillObj.pulse = measure.Pluse;
                                specialbillObj.breath = measure.Breath;
                                specialbillObj.hr = measure.HeartRate;
                            }
                        }
                    }
                    else
                    {
                        specialbillObj.temper = " ";
                        specialbillObj.pulse = " ";
                        specialbillObj.breath = " ";
                        specialbillObj.hr = " ";
                    }
                    if (!string.IsNullOrWhiteSpace(footerMea.Blood_Pressure))
                    {
                        if (footerMea.Blood_Pressure.Contains('/'))
                        {
                            int a = footerMea.Blood_Pressure.IndexOf('/');
                            specialbillObj.systolicpressure = footerMea.Blood_Pressure.Substring(0, a); ;
                            specialbillObj.diastolicpressure = footerMea.Blood_Pressure.Substring(a + 1);
                        }
                    }
                    else
                    {
                        specialbillObj.systolicpressure = " ";
                        specialbillObj.diastolicpressure = " ";
                    }
                    specialbillObj.spo2 = " ";
                    intake intakeObj1 = new intake(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "2", "1", "总入量", footerMea.Liquid_In, "ml", " ", " ", " ");
                    discharge dischargeObj1 = new discharge(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "3", "1", "总出量", footerMea.Liquid_Out, "ml", " ");
                    specialbillele specialbilleleObj1 = new specialbillele(" ", " ", " ", " ");
                    intakeList.Add(intakeObj1);
                    dischargeList.Add(dischargeObj1);
                    specialbilleleList.Add(specialbilleleObj1);
                    specialbillList.Add(specialbillObj);
                    specialbills.specialbill = specialbillList;
                    specInfoObj.specialbills = specialbills;
                    specInfoObj.request = requestObj;
                    string bodyXml = XmlUtilTool.XmlSerialize(specInfoObj, Encoding.UTF8, true, false);
                    YDHLService.ManageZYServiceService insertEMRService = new YDHLService.ManageZYServiceService();
                    string result = insertEMRService.intoEMR(bodyXml);
                    XmlDocument doc = XmlUtilTool.ConvertStringToXmlDocument(result);
                    XmlNode codeNode = doc.SelectSingleNode("message/response/code");
                    XmlNode descriptionNode = doc.SelectSingleNode("message/response/description");
                    if (codeNode != null)
                    {
                        if (!codeNode.InnerText.Equals("0"))
                        {
                            string returnString = string.Format("病人名-{0},测量时间{1},PC端体温单回写EMR库成功", temuser.PatientId.Trim(), footerMea.ValueDate.ToString("yyyymmdd"));
                            WriteLog.WriteLogString(returnString);
                        }
                    }
                }
            }
        }

        #region 根据住院号等查询自定义项目名称
        /// <summary>
        /// 根据住院号等查询自定义项目名称
        /// </summary>
        /// <param name="PatientId"></param>
        /// <param name="VISIT_ID"></param>
        /// <param name="PageIndex"></param>
        /// <param name="BlankId"></param>
        /// <returns></returns>
        public string GetBlankNameByPatientId(string PatientId, string VISIT_ID, int PageIndex, string BlankId)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append(" SELECT [BlankName]  FROM [t_temperature_blank](NOLOCK) where 1=1");
            if (!string.IsNullOrWhiteSpace(PatientId))
            {
                strb.Append(" and PatientId='" + PatientId + "'");
            }
            if (!string.IsNullOrWhiteSpace(VISIT_ID))
            {
                strb.Append(" and VISIT_ID='" + VISIT_ID + "'");
            }
            if (PageIndex > 0)
            {
                strb.Append(" and PageIndex='" + PageIndex + "'");
            }
            if (!string.IsNullOrWhiteSpace(BlankId))
            {
                strb.Append(" and BlankId='" + BlankId + "'");
            }
            return SqlHelper.ExecuteScalar(CommandType.Text, strb.ToString()).ToString();
        }
        #endregion

        #endregion

        #region 泉州护理记录单、血糖记录单数据回写EMR库

        /// <summary>
        /// 同步护理记录单到数据到海泰的异步委托方法
        /// <param name="insertflag">新增1，修改2，删除3</param>
        /// <param name="formType">* 001，（神内）护理记录单；002，（全院通用）护理记录单；</param>
        /// </summary>
        public void clinicalNursingInfoAsyncToEMRDelegateMethod(ClinicalNursinguser cnuser, string formType, string insertflag = "")
        {
            clinicalNursingInfoAsyncToEMRDelegate asyncDelegate = new clinicalNursingInfoAsyncToEMRDelegate(clinicalNursingInfoAsyncToEMR);
            //将我们系统工号转成海泰系统工号
            string userID = getUSERIDBypersonnelId(cnuser.UpdateuserId);
            string huserID = getHUSERIDByUSERID(userID);
            if (!string.IsNullOrWhiteSpace(huserID))
            {
                cnuser.UpdateuserId = huserID;
            }
            asyncDelegate.BeginInvoke(cnuser, cnuser.data.Replace("NursingRecordDocument", "ClinicalNursingModel"), formType, insertflag, null, null);
        }

        #region 根据从HIS的V_YDHL_USER取USER_ID
        public string getUSERIDBypersonnelId(string personnelId)
        {
            string sql = " SELECT [USER_ID] FROM [V_YDHL_USER](NOLOCK) where [IC_CARD_ID]='" + personnelId + "'";
            DataSet ds = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text, SQLDLL.Base.HISConnStr, null);
            if (ds == null || ds.Tables.Count <= 0) return string.Empty;
            DataTable dt = ds.Tables[0];
            if (dt == null || dt.Rows.Count <= 0) return string.Empty;
            return dt.Rows[0]["USER_ID"].ToString();
        }
        #endregion

        #region 根据USER_ID从海泰取H_USER_ID
        public string getHUSERIDByUSERID(string userId)
        {
            string sql = "SELECT H_USER_ID FROM V_YDHL_USER_COMPARISON where USER_ID='" + userId + "'";
            DataTable dt = OracleHelper.ExecToSqlGetTable(sql, SQLDLL.Base.HTConnStr);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["H_USER_ID"].ToString();
            }
            else
            {
                return "";
            }
        }

        #endregion

        /// <summary>
        /// 同步护理记录单数据到海泰EMR库
        /// </summary>
        /// <param name="clinicalNursing"></param>
        /// <param name="clinicalNursingxmlStr"></param>
        /// <param name="insertflag">新增1，修改2，删除3</param>
        public void clinicalNursingInfoAsyncToEMR(ClinicalNursinguser clinicalNursing, string clinicalNursingxmlStr, string formType, string insertflag = "")
        {
            ClinicalNursingModel clinicalNursingObj = XmlUtilTool.DeserializeToObject<ClinicalNursingModel>(clinicalNursingxmlStr);
            SpecialbillInfo specInfoObj = new SpecialbillInfo();
            specialbills specialbills = new specialbills();
            List<specialbill> specialbillList = new List<specialbill>();
            specialbill specialbillObj = new specialbill();
            List<intake> intakeList = new List<intake>();
            List<discharge> dischargeList = new List<discharge>();
            List<specialbillele> specialbilleleList = new List<specialbillele>();
            request requestObj = new request();
            requestObj.targetLogicService = "EMR_SPECIAL_BILL_INFO";
            string specialconfigsn = " ";
            /*
             * 001，（神内）护理记录单；002，（全院通用）护理记录单；003 (待产、产程)；
             * 100，（神内）24小时入出量；101，（神内）12小时入出量；
             * 200，（全院通用）24小时入出量；201，（全院通用）12小时入出量；
             * */
            if (formType == "001")
            {
                requestObj.targetLogicApp = "001";
                specialconfigsn = "13630069866070145882";
            }
            else if (formType == "002")
            {
                requestObj.targetLogicApp = "002";
                specialconfigsn = "13539761728360413173";
            }
            else if (formType == "003")
            {
                requestObj.targetLogicApp = "003";
                specialconfigsn = "13566102823290355344";
            }
            specialbillObj.intake = intakeList;
            specialbillObj.discharge = dischargeList;
            specialbillObj.specialbillele = specialbilleleList;
            if (string.IsNullOrWhiteSpace(insertflag))
            {
                specialbillObj.insertflag = "1";//新增1，修改2，删除3
            }
            else
            {
                specialbillObj.insertflag = insertflag;//新增1，修改2，删除3
            }
            specialbillObj.nodetype = "0";//0,普通; 1,24小时总出入量; 2,24小时尿量; 3,24时出量; 4,12时总入出量;*5,12小时尿量; 6,12小时出量;
            specialbillObj.specialconfigsn = specialconfigsn;
            specialbillObj.specialsn = clinicalNursing.syncSN;
            specialbillObj.inpatientid = clinicalNursing.PatientId.TrimEnd() + clinicalNursing.VisitId + "_" + clinicalNursing.VisitId;
            specialbillObj.patientid = clinicalNursing.PatientId.TrimEnd();
            string sql = string.Format("select * from v_ydhl_user_comparison where HUSER_NAME='{0}'", clinicalNursing.NursName);
            DataTable dt = OracleHelper.ExecToSqlGetTable(sql, SQLDLL.Base.HTConnStr);
            if (dt.Rows.Count > 0)
            {
                clinicalNursing.UpdateuserId = dt.Rows[0]["H_USER_ID"].ToString();
                clinicalNursing.UpdateuserName = dt.Rows[0]["USER_NAME"].ToString();
            }
            specialbillObj.creator = clinicalNursing.UpdateuserId;
            specialbillObj.creatortime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            specialbillObj.modifier = clinicalNursing.UpdateuserId;
            specialbillObj.modifiertime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            if (!string.IsNullOrEmpty(insertflag) && !insertflag.Equals("3"))
            {
                specialbillObj.recordtime = Convert.ToDateTime(clinicalNursing.Date + " " + clinicalNursing.Time + ":00").ToString("yyyyMMddHHmmss");
            }
            specialbillObj.recorder = clinicalNursing.UpdateuserName;
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Tendcontent))
            {
                specialbillObj.tendcontent = clinicalNursingObj.Tendcontent;
            }
            else
            {
                specialbillObj.tendcontent = "";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Temper))
            {
                specialbillObj.temper = clinicalNursingObj.Temper;
            }
            else
            {
                specialbillObj.temper = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Pulse))
            {
                specialbillObj.pulse = clinicalNursingObj.Pulse;
            }
            else
            {
                specialbillObj.pulse = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Breath))
            {
                specialbillObj.breath = clinicalNursingObj.Breath;
            }
            else
            {
                specialbillObj.breath = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.HeartRate))
            {
                specialbillObj.hr = clinicalNursingObj.HeartRate;
            }
            else
            {
                specialbillObj.hr = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Systolicpressure))
            {
                specialbillObj.systolicpressure = clinicalNursingObj.Systolicpressure;
            }
            else
            {
                specialbillObj.systolicpressure = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Diastolicpressure))
            {
                specialbillObj.diastolicpressure = clinicalNursingObj.Diastolicpressure;
            }
            else
            {
                specialbillObj.diastolicpressure = " ";
            }
            if (!string.IsNullOrWhiteSpace(clinicalNursingObj.Spo2))
            {
                specialbillObj.spo2 = clinicalNursingObj.Spo2;
            }
            else
            {
                specialbillObj.spo2 = " ";
            }
            specialbillObj.deptcode = clinicalNursing.DepId;
            specialbillObj.deptname = clinicalNursing.DepName;
            specialbillObj.wardname = clinicalNursing.AreaName;
            specialbillObj.wardcode = clinicalNursing.AreaId;
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Intakename))
            {
                clinicalNursingObj.Intakename = "";
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Intakevalue))
            {
                clinicalNursingObj.Intakevalue = "";
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Intakediway))
            {
                clinicalNursingObj.Intakediway = "";
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Dischargename))
            {
                clinicalNursingObj.Dischargename = "";
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Dischargevalue))
            {
                clinicalNursingObj.Dischargevalue = "";
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Intakename) && string.IsNullOrWhiteSpace(clinicalNursingObj.Intakevalue) && string.IsNullOrWhiteSpace(clinicalNursingObj.Intakediway))
            {

            }
            else
            {
                if (clinicalNursingObj.Intakename.Contains(';'))
                {
                    for (int i = 0; i < clinicalNursingObj.Intakename.Split(';').Count(); i++)
                    {
                        string Intakename = clinicalNursingObj.Intakename.Split(';')[i];
                        string Intakevalue = clinicalNursingObj.Intakevalue.Split(';')[i];
                        string Intakediway = clinicalNursingObj.Intakediway.Split(';')[i];
                        intake intakeObj1 = new intake(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "4" + i, "1", Intakename, Intakevalue, "ml", Intakediway, "", "");
                        intakeList.Add(intakeObj1);
                    }
                }
                else
                {
                    intake intakeObj1 = new intake(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "2", "1", clinicalNursingObj.Intakename, clinicalNursingObj.Intakevalue, "ml", clinicalNursingObj.Intakediway, "", "");
                    intakeList.Add(intakeObj1);
                }
            }
            if (string.IsNullOrWhiteSpace(clinicalNursingObj.Dischargename) && string.IsNullOrWhiteSpace(clinicalNursingObj.Dischargevalue))
            {

            }
            else
            {
                if (clinicalNursingObj.Dischargename.Contains(';'))
                {
                    for (int i = 0; i < clinicalNursingObj.Dischargename.Split(';').Count(); i++)
                    {
                        string dischargename = clinicalNursingObj.Dischargename.Split(';')[i];
                        string dischargevalue = clinicalNursingObj.Dischargevalue.Split(';')[i];
                        discharge dischargeObj1 = new discharge(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "4" + i, "1", dischargename, dischargevalue, "ml", "");
                        dischargeList.Add(dischargeObj1);
                    }
                }
                else
                {
                    discharge dischargeObj1 = new discharge(System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "3", "1", clinicalNursingObj.Dischargename, clinicalNursingObj.Dischargevalue, "ml", "");
                    dischargeList.Add(dischargeObj1);
                }
            }

            if (!formType.Equals("003"))
            {
                specialbillele ele = new specialbillele("0", "HT51.02.677", "翻身", !string.IsNullOrEmpty(clinicalNursingObj.StandUp) ? "√" : "");
                specialbilleleList.Add(ele);
            }
            foreach (var item in clinicalNursingObj.specialbillele)
            {
                specialbillele ele = new specialbillele(item.option, item.eleid, item.elename, item.elevalue);
                specialbilleleList.Add(ele);
            }
            specialbillList.Add(specialbillObj);
            specialbills.specialbill = specialbillList;
            specInfoObj.specialbills = specialbills;
            specInfoObj.request = requestObj;
            string bodyXml = XmlUtilTool.XmlSerialize(specInfoObj, Encoding.UTF8, true, false);
            WriteLog.WriteLogString(bodyXml);
            YDHLService.ManageZYServiceService insertEMRService = new YDHLService.ManageZYServiceService();
            string result = insertEMRService.intoEMR(bodyXml);
            XmlDocument doc = XmlUtilTool.ConvertStringToXmlDocument(result);
            XmlNode codeNode = doc.SelectSingleNode("message/response/code");
            XmlNode descriptionNode = doc.SelectSingleNode("message/response/description");
            if (codeNode != null)
            {
                if (!codeNode.InnerText.Equals("0"))
                {
                    string returnString = string.Format("病人名-{0},测量时间{1},PC端护理记录单回写EMR库", clinicalNursing.PatientId.TrimEnd(), clinicalNursing.Date + clinicalNursing.Time);
                    WriteLog.WriteLogString(returnString);
                }
            }
        }

        /// <summary>
        /// 同步血糖记录单到数据到海泰的异步委托方法
        /// </summary>
        ///  syncSN: 专科记录流水号 唯一值
        public void bloodsugarInfoAsyncToEMRDelegateMethod(ClinicalNursinguser cnuser, string insertflag = "")
        {
            bloodsugarInfoASyncToEMRDelegate asyncDelegate = new bloodsugarInfoASyncToEMRDelegate(bloodsugarInfoAsyncToEMR);
            asyncDelegate.BeginInvoke(cnuser, cnuser.data.Replace("BloodSugarRecordDocument", "BloodsugarModel"), insertflag, null, null);
        }
        /// <summary>
        /// 同步血糖记录单到数据到海泰
        /// </summary>
        public void bloodsugarInfoAsyncToEMR(ClinicalNursinguser clinicalNursing, string bloodsugarxmlStr, string insertflag = "")
        {
            BloodsugarModel bloodsugarObj = XmlUtilTool.DeserializeToObject<BloodsugarModel>(bloodsugarxmlStr);
            GridbillInfo gridbillInfoObj = new GridbillInfo();
            gridbills gridbillsObj = new gridbills();
            gridbill gridbillObj = new gridbill();
            List<gridbill> gridbillList = new List<gridbill>();
            List<gridbillele> gridbilleleList = new List<gridbillele>();
            request requestObj = new request("EMR_GRID_BILL_INFO", "003");
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.WeeHour))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.999", "血糖", bloodsugarObj.WeeHour);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.breadfastbefore))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.714.01", "血糖（早餐前）", bloodsugarObj.breadfastbefore);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.breadfastafter))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.714.02", "血糖（早餐后2h）", bloodsugarObj.breadfastafter);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.lunchbefore))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.715.01", "血糖（中餐前）", bloodsugarObj.lunchbefore);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.lunchafter))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.715.02", "血糖（中餐前）", bloodsugarObj.lunchafter);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.dinnerbefore))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.716.01", "血糖（晚餐前）", bloodsugarObj.dinnerbefore);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.dinnerafter))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.716.02", "血糖（晚餐后2h）", bloodsugarObj.dinnerafter);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.bedtime))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.1155", "睡前血糖", bloodsugarObj.bedtime);
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.sugar1))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.816", "血糖其他1", bloodsugarObj.sugar1 + "/" + Convert.ToDateTime(bloodsugarObj.sugartime1).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.sugar2))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.817", "血糖其他2", bloodsugarObj.sugar2 + "/" + Convert.ToDateTime(bloodsugarObj.sugartime2).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.sugar3))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.818", "血糖其他3", bloodsugarObj.sugar3 + "/" + Convert.ToDateTime(bloodsugarObj.sugartime3).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.sugar4))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.819", "血糖其他4", bloodsugarObj.sugar4 + "/" + Convert.ToDateTime(bloodsugarObj.sugartime4).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.ketone1))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.857", "血酮时间1", bloodsugarObj.ketone1 + "/" + Convert.ToDateTime(bloodsugarObj.ketonetime1).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            if (!string.IsNullOrWhiteSpace(bloodsugarObj.ketone2))
            {
                gridbillele gridbilleleObj = new gridbillele("HT51.02.692", "血酮时间2", bloodsugarObj.ketone2 + "/" + Convert.ToDateTime(bloodsugarObj.ketonetime2).ToString("HH:mm"));
                gridbilleleList.Add(gridbilleleObj);
            }
            gridbillObj.gridbillele = gridbilleleList;
            gridbillObj.gridconfigsn = "13462363430730692490";
            gridbillObj.gridbillsn = clinicalNursing.syncSN;//专科记录流水号 唯一值
            gridbillObj.inpatientid = clinicalNursing.PatientId.Trim() + clinicalNursing.VisitId + "_" + clinicalNursing.VisitId;
            gridbillObj.patientid = clinicalNursing.PatientId.Trim();
            if (string.IsNullOrWhiteSpace(insertflag))
            {
                gridbillObj.insertflag = "1";//新增1，修改2，删除3
            }
            else
            {
                gridbillObj.insertflag = insertflag;//新增1，修改2，删除3
            }
            string sql = string.Format("select * from v_ydhl_user_comparison where HUSER_NAME='{0}'", clinicalNursing.NursName);
            DataTable dt = OracleHelper.ExecToSqlGetTable(sql, SQLDLL.Base.HTConnStr);
            if (dt.Rows.Count > 0)
            {
                clinicalNursing.UpdateuserId = dt.Rows[0]["H_USER_ID"].ToString();
                clinicalNursing.UpdateuserName = dt.Rows[0]["H_USER_ID"].ToString();
            }
            gridbillObj.creator = clinicalNursing.UpdateuserId;
            gridbillObj.creatortime = clinicalNursing.Updatetime.ToString("yyyyMMddHHmmss");
            gridbillObj.modifier = clinicalNursing.UpdateuserId;
            gridbillObj.modifiertime = clinicalNursing.Updatetime.ToString("yyyyMMddHHmmss");
            gridbillObj.recordtime = Convert.ToDateTime(clinicalNursing.Date).ToString("yyyyMMddHHmmss");
            gridbillObj.recorder = clinicalNursing.UpdateuserName.ToString();
            gridbillObj.deptcode = clinicalNursing.DepId;
            gridbillObj.deptname = clinicalNursing.DepName;
            gridbillObj.wardname = clinicalNursing.AreaName;
            gridbillObj.wardcode = clinicalNursing.AreaId;
            gridbillList.Add(gridbillObj);
            gridbillsObj.gridbill = gridbillList;
            gridbillInfoObj.gridbills = gridbillsObj;
            gridbillInfoObj.request = requestObj;
            string bodyXml = XmlUtilTool.XmlSerialize(gridbillInfoObj, Encoding.UTF8, true, false);
            YDHLService.ManageZYServiceService insertEMRService = new YDHLService.ManageZYServiceService();
            string result = insertEMRService.intoEMR(bodyXml);
            XmlDocument doc = XmlUtilTool.ConvertStringToXmlDocument(result);
            XmlNode codeNode = doc.SelectSingleNode("message/response/code");
            XmlNode descriptionNode = doc.SelectSingleNode("message/response/description");
            if (codeNode != null)
            {
                if (!codeNode.InnerText.Equals("0"))
                {
                    string returnString = string.Format("病人名-{0},测量时间{1},PC端血糖单回写EMR库", clinicalNursing.PatientId.TrimEnd(), clinicalNursing.Date + clinicalNursing.Time);
                    WriteLog.WriteLogString(returnString);
                }
            }
        }
        #endregion
    }
}
